//
//  FriendRequest.m
//  极致荟
//
//  Created by hetao on 14-8-29.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "FriendRequest.h"
#import "VXSqli.h"
@implementation FriendRequest

///添加好友验证记录
+ (void)insertData:(FriendRequest *)fr
{
    FMDatabase *db=[VXSqli shareVxsqlit].db;
       FMResultSet *set= [db executeQuery:@"select * from FriendRequest where MyId=? and FriendId=?",[NSNumber numberWithInt:[User defuceUser].userId],[NSNumber numberWithInt:fr.userId]];
    if(!set.next)
    {
        BOOL isadd=  [db executeUpdate:@"insert into FriendRequest values(?,?,?,?,?,?,?,?)",[NSNumber numberWithInt:[User defuceUser].userId],[NSNumber numberWithInt:fr.userId],fr.name,fr.userHeadImage,[NSNumber numberWithInt:fr.requestType],[NSNumber numberWithBool:fr.isSee],[NSNumber numberWithLongLong:[fr.requesetDate timeIntervalSince1970]] ,fr.rpId];
        if(isadd)
        {
            NSLog(@"添加好友请求成功");
        }else
        {
            NSLog(@"添加好友请求失败");
            
        }
    }else
    {
        [self upData:fr];
    }
 
}

///查询所有好友请求
+ (NSMutableArray *)selectFriend
{
    FMDatabase *db=[VXSqli shareVxsqlit].db;
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    FMResultSet *set=[db executeQuery:@"select * from FriendRequest  where MyId=? order by requesetDate desc",[NSNumber numberWithInt:[User defuceUser].userId]];
    while (set.next)
    {
        FriendRequest *ft=[[FriendRequest alloc] init];
        ft.userId=[set intForColumn:@"FriendId"];
        ft.userHeadImage=[set stringForColumn:@"FriendHead"];
        ft.name=[set stringForColumn:@"FriendName"];
        ft.requestType=[set intForColumn:@"Result"];
        ft.isSee=[set boolForColumn:@"IsSee"];
        ft.requesetDate=[NSDate dateWithTimeIntervalSince1970:[set longLongIntForColumn:@"requesetDate"]];
        ft.rpId=[set stringForColumn:@"FRId"];
        if(ft.userId!=[User defuceUser].userId)
        {
            [arr addObject: ft];
        }
        
    }
    [set close];
    return arr;
}

///查询为查看好友请求的记录数
+ (int)selectRN
{
    FMDatabase *db=[VXSqli shareVxsqlit].db;
    FMResultSet *set=[db executeQuery:@"select IsSee from FriendRequest where IsSee=0 and MyId=?",[NSNumber numberWithInt:[User defuceUser].userId]];
    int i=0;
    while ([set next]) {
        i++;
    }
    [set close];
    return  i;
}

///修改好友请求数据
+ (void)upData:(FriendRequest *)fr
{
    FMDatabase *db=[VXSqli shareVxsqlit].db;
    [db executeUpdate:@"update FriendRequest set FRId=?,FriendName=?,FriendHead=? ,Result=?,requesetDate=? where FriendId=? and MyId=?",fr.rpId,fr.name,fr.userHeadImage,[NSNumber numberWithInt:fr.requestType],[NSNumber numberWithInt:[fr.requesetDate timeIntervalSince1970]],[NSNumber numberWithInt:fr.userId],[NSNumber numberWithInt:[User defuceUser].userId]];
}

///清空所有未查看消息记录
+ (void)dorpRN
{
    FMDatabase *db=[VXSqli shareVxsqlit].db;
    [db executeUpdate:@"update FriendRequest set IsSee=1 where MyId=?",[NSNumber numberWithInt:[User defuceUser].userId]];
}
///清空所有好友请求
+ (void)dorpRNList
{
    FMDatabase *db=[VXSqli shareVxsqlit].db;
    [db executeUpdate:@"delete from FriendRequest  where MyId=?",[NSNumber numberWithInt:[User defuceUser].userId]];
}
///从网络上获取好友请求数据
+(void)HttpFriendRequestcomYes:(void (^)(NSMutableArray *, NSString *))comyes
{
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId", nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"friends/respondList") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        if(rerurncode==0)
        {
            NSMutableArray *dataarr=[[NSMutableArray alloc] init];
            NSLog(@"%@",data);
            for(NSDictionary *datadic in [[data objectForKey:@"data"] objectForKey:@"list"])
            {
                FriendRequest *ouser=[[FriendRequest alloc]init];
                ouser.userId=[[datadic objectForKey:@"pushUserId"] intValue];
                ouser.userHeadImage=[[[data objectForKey:@"data"] objectForKey:@"imgPrefix"] stringByAppendingString:[datadic objectForKey:@"pushUserAvatar"]];
                ouser.rpId=[datadic objectForKey:@"attentionId"];
                ouser.requestType=[[datadic objectForKey:@"Status"] intValue];
                if([NSNull null]==[datadic objectForKey:@"pushUserName"])
                {
                    ouser.name=@"暂未获取";
                }else
                {
                    ouser.name=[datadic objectForKey:@"pushUserName"];
                }
                ouser.isSee=NO;
                ouser.requesetDate=[NSDate dateWithTimeIntervalSince1970:[[datadic objectForKey:@"pushTime"] longLongValue]];
                [self insertData:ouser];
                if(ouser.userId!=[User defuceUser].userId)
                {
                    [dataarr addObject:ouser];
                }
                
            }
            
            comyes(dataarr,[[data objectForKey:@"data"] objectForKey:@"lastPushTime"] );
        }
    }];

}

@end
